home *** CD-ROM | disk | FTP | other *** search
- /*
- * gplot.c - A demo driver for the gnuplotio library.
- *
- * Nov 28, 1991
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include "gnuplotio.h"
- #include "util.h" /* print_nonprint() */
-
- void main(int argc, char *argv[])
- {
- GNUPLOT *gp;
- #define N 10
- char buf[N];
- int curve,points;
- int s;
- float *x,*y,*z;
- char *range;
- int i;
- GPCurve *gc;
-
- /* Open gnuplot connection */
- if ((gp = openGnuplot(argv[1])) == NULL)
- {
- fprintf(stderr,"openGnuplot failed :(\n");
- exit(EXIT_FAILURE);
- }
-
- /* Enter evaluator mode */
- s=writeGnuplot(gp, "set term table\n");
- if (s == 0) fprintf(stderr,"err writeGnuplot\n");
- s=writeGnuplot(gp, "set samples 10\n");
- if (s == 0) fprintf(stderr,"err writeGnuplot\n");
-
- /* Write a bogus command to test error reporting */
- s=writeGnuplot(gp, "pluck\n");
- s=readErrorGnuplot(gp, buf, sizeof(buf));
- if(s == -1) fprintf(stderr,"err readError\n");
- else if (s > 0)
- { printf("Error buf (n=%d):\n",s);
- print_nonprint(buf,HOW_ESCAPE);
- print_nonprint(buf,HOW_OCTAL);
- print_nonprint(buf,HOW_HEX);
- print_nonprint(buf,HOW_QUES);
- }
-
- /* Send a plot command */
- s=writeGnuplot(gp, "plot sin(x)");
- if (s == 0) fprintf(stderr,"err writeGnuplot 2\n");
-
- /* Read curve piecemeal */
- s=readCurveHeadGnuplot(gp, &curve, &points);
- if (s == 0) fprintf(stderr,"err readCurveHead\n");
-
- printf("Curve %d has %d points\n",curve,points);
- /* Alloc buffers */
- x = (float *) malloc(sizeof(float) * (size_t)points);
- y = (float *) malloc(sizeof(float) * (size_t)points);
- range = (char *) malloc(sizeof(char) * (size_t)points);
-
- /* Read 2D data */
- s=readCurve2Gnuplot(gp, NULL, x,y,points);
- if (s == 0) fprintf(stderr,"err readCurve2\n");
-
- /* Dump data */
- for (i=0; i<points; i++)
- printf("%g %g\n",x[i],y[i]);
-
- /* Check in for any error messages */
- s=readErrorGnuplot(gp, buf, sizeof(buf));
- if(s == -1) fprintf(stderr,"err readError\n");
- else if (s > 0) /*printf("Error buf (n=%d): \"%s\"\n",s,buf);*/
- { printf("Error buf (n=%d):\n",s);
- print_nonprint(buf,HOW_ESCAPE);
- }
-
- /* Now do a 3D plot */
- s=writeGnuplot(gp, "splot sin(x)**2");
- if (s == 0) fprintf(stderr,"err writeGnuplot 3\n");
-
- /* But read it the easy way */
- if ((gc = readCurveGnuplot(gp)) != NULL)
- {
- /* Dump data */
- printf("curveno=%d is_2d=%d npts=%u\n",gc->curveno,gc->is_2d,
- gc->npts);
- for (i=0; i<gc->npts; i++)
- {
- if (gc->is_2d)
- printf("%c %g %g\n",gc->range[i],gc->x[i],gc->y[i]);
- else
- printf("%c %g %g %g\n",gc->range[i],gc->x[i],gc->y[i],
- gc->z[i]);
- }
- }
-
- /* Close gnuplot connection */
- s=closeGnuplot(gp,1);
- if (s == 0) fprintf(stderr,"err close\n");
- }
-
-